package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Orders;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("orders:{}",orders);

        //1.调用service处理
        ordersService.sumbit(orders);

        //2.响应处理结果
        return R.success("下单成功！");
    }

    //客户端查看订单
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> look(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int pageSize) {
        Page<OrdersDto> orders = ordersService.look(page, pageSize);

        return R.success(orders);
    }

    //订单分页查询
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime) {
        Page<Orders> dtoPage= ordersService.pages(page,pageSize,number,beginTime,endTime);
        return R.success(dtoPage);
    }

    //订单派送/完成
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getId,orders.getId());
        ordersService.update(orders,qw);
        return R.success("跟新状态成功~~~~");

    }
}
